<?php
/**
 * Talks to database about pets
 * Ross Mathis
 */

class Pet extends Model {
        /*
         * View all pets in database
         */
       public function view_pets()
       {
        $this->load->database();

        return $this->db->query("SELECT name, p.species, p.breed, p.pet_id, o.fname, o.lname FROM pets p INNER JOIN owners o ON p.owner_id=o.owner_id;")->result();
       }
       //---------------------------------------
       /*
        * Retrieve details for a specific pet
        * @param $where
        */
       public function retrieve_details($where)
       {
           $sql = "SELECT o.*, p.* FROM owners o INNER JOIN pets p ON o.owner_id = p.owner_id where p.pet_id = ?;";
	return $this->db->query($sql,array($where))->result();
       }
       //------------------------------------
       /*
        * Query database for pet and their owners and order by pet name
        */
        public function orderby_pets()
        {

         return $this->db->query('SELECT owner.*, pets.*  FROM owners INNER JOIN pets ON owner.owner_id = pets.owner_id GROUP BY pets.pet_name ORDER BY pets.pet_name;')->result();

        }
        //------------------------------
        /*
         * Query database for pets and their owners and order by species
         */
         public function orderby_species()
        {

         return $this->db->query('SELECT owner.*, pets.*  FROM owners INNER JOIN pets ON owner.owner_id = pets.owner_id GROUP BY pets.pet_id ORDER BY pets.species;')->result();

        }
        //--------------------------------
        /*
         * Query database for pets and their owners and order by breed
         */
         public function orderby_breed()
        {

         return $this->db->query('SELECT owner.*, pets.* FROM owners INNER JOIN pets ON owner.owner_id = pets.owner_id GROUP BY pets.pet_id ORDER BY UPPER(pets.breed);')->result();

        }
        //----------------------------------
        /*
         * Insert a new pet into the database
         * @param $pet_name
         * @param $species
         * @param $breed
         * @param $weight
         * @param $coloring
         * @param $knownconditions
         * @param $notes
         * @param $id
         */
        public function insert_pets($pet_name,$species,$breed,$weight,$coloring,$knownconditions,$notes,$id){
            $data = array('name'=>$name, 'species'=>$species, 'breed'=>$breed, 'weight'=>$weight, 'coloring'=>$coloring, 'known_conditions'=>$knownconditions, 'pet_notes'=>$notes, 'owner_id'=>$id);

            $str = $this->db->insert_string('pets', $data);

            $query = $this->db->query($str);


		}
           //----------------------------------
        /*
         * Insert a new pet into the database
         * @param $pet_name
         * @param $species
         * @param $breed
         * @param $weight
         * @param $coloring
         * @param $knownconditions
         * @param $notes
         * @param $id
         */
          public function update_pets($pet_name,$species,$breed,$weight,$coloring,$knownconditions,$notes,$id){
            $data = array('name'=>$pet_name, 'species'=>$species, 'breed'=>$breed, 'weight'=>$weight, 'coloring'=>$coloring, 'know_conditions'=>$knownconditions, 'pet_notes'=>$notes);

            $where = "pet_id = $id";

            $str = $this->db->update_string('pets', $data, $where);

            $query = $this->db->query($str);


		}
}
